<?php
namespace app\admin\controller;
use app\admin\controller\Login;
use think\Controller;
use think\Db;
// use app\index\
class Index extends Base
{
    public function index()
    {
		return view('index');
    }
    public function main()
    {
		return view('main');
    }
    public function home()
    {
        return view('home');
    }
    public function layouts()
    {
        $server['php_version']=PHP_VERSION;
        $server['php_os']=PHP_OS;
        $server['server_addr']=$_SERVER ['SERVER_ADDR'];
        $server['mysql_version']=db()->query("select version() as mysql_version")[0]['mysql_version'];
        $server['think_version']=THINK_VERSION;
        $server['login_version']=$_SERVER["REMOTE_ADDR"];
        // print_r($server);exit;
        $this->assign("server",$server);
        return view('layouts');
    }
    // 构造树形父子结构
    protected function get_tree($arr, $pid = 0, $id = 'id', $pname = 'pid', $child = 'children')
	{
	    $tree = array();
	    foreach ($arr as $value) {
	        if ($value[$pname] == $pid) {
	            $value[$child] = $this->get_tree($arr, $value[$id], $id = 'id', $pname = 'pid', $child = 'children');
	            if ($value[$child] == null) {
	                unset($value[$child]);
	            }
	            $tree[] = $value;
	        }
	    }
	    return $tree;
	}
    // 按照顺序构造一级数组，根据level判断层级
    public function getTree($array, $pid =0, $level = 0){

        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level+1);

            }
        }
        return $list;
    }
}
